package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.OrderBean;
import com.project.bean.OrderInfoBean;
import com.project.mapper.OrderInfoMapper;

import com.project.mapper.UserOrderMapper;
import com.project.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 订单表（用户模块） 服务实现类
 * </p>
 *
 * @author lhy
 * @since 2021-10-05
 */
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private UserOrderMapper orderMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public OrderBean addOrder(List<OrderInfoBean> infoList, OrderBean orderBean) {
        double orderMoney = 0;
        for (int i = 0; i < infoList.size(); i++) {
            OrderInfoBean info = infoList.get(i);
            double productPrice = Double.parseDouble(info.getProductPrice());
            double infoPrice = productPrice * info.getBuyNum();
            orderMoney += infoPrice;
        }
        String orderMoneyStr = orderMoney + "";
        orderBean.setOrderMoney(orderMoneyStr);
        orderMapper.insert(orderBean);
        orderInfoMapper.addInfoList(infoList, orderBean.getOrderCode());
        orderBean.setInfoList(infoList);
        return orderBean;
    }

    @Override
    public OrderBean updateOrderStatus(String orderCode, int status) {
        orderMapper.updateOrderStatus(orderCode, status);
        return orderMapper.findByOrderCode(orderCode);
    }

    @Override
    public List<OrderBean> findOrderByItem(Integer status, String userCode) {
        if(status!=null && status==1){
            return orderMapper.findOrderByItemUnFinish(status,userCode);
        }
        return orderMapper.findOrderByItem(status, userCode);
    }

    @Override
    public OrderBean findByOrderCode(String orderCode) {
        return orderMapper.selectOne(new QueryWrapper<OrderBean>().eq("orderCode", orderCode));
    }

    @Override
    public void addOrderRiderAndUpdateStatus(String orderCode, String riderCode, String riderName, int status) {
        orderMapper.addOrderRiderAndUpdateStatus(orderCode,riderCode,riderName,status);
    }

    @Override
    public OrderBean finishOrderAndUpdateStatus(String orderCode, int status, String receiveTime) {
        orderMapper.finishOrderAndUpdateStatus(orderCode,status,receiveTime);
        return orderMapper.findByOrderCode(orderCode);
    }

    @Override
    public void updateOrderStarStatus(String orderCode) {
        orderMapper.updateOrderStarStatus(orderCode);
    }

}
